from universal.common_header import *
__appname__ = '章鱼博士'
def lineEdit_act(self):  #打开文件夹
    act = QAction(self)
    act.setIcon(QIcon(':icons/1-1.ico'))
    EditPath_act = QLineEdit('')
    act.triggered.connect(lambda: SaveDirPATH(self,EditPath_act))
    EditPath_act.addAction(act, QLineEdit.TrailingPosition)
    return EditPath_act


def lineEdit_act_s(self):  #可以选择多次文件夹不会覆盖
    act = QAction(self)
    act.setIcon(QIcon(':icons/1-1.ico'))
    EditPath_act = QLineEdit('')
    act.triggered.connect(lambda: SaveDirPATHs(self,EditPath_act))
    EditPath_act.addAction(act, QLineEdit.TrailingPosition)
    return EditPath_act

def Table_widget(self,row,column):
    TabelParameter = QTableWidget(self)
    TabelParameter.setColumnCount(column)
    TabelParameter.setRowCount(row)
    TabelParameter.horizontalHeader().setStretchLastSection(True)

    TabelParameter.verticalHeader().setVisible(False)
    TabelParameter.horizontalHeader().setVisible(False)
    return TabelParameter


def Table_widget_setItem(widget,name_list):
    name_List_back = []
    for i in name_list:
        name = QTableWidgetItem(i)
        name_List_back.append(name)
    for i in range(len(name_List_back)):
        widget.setItem(i, 0, name_List_back[i])
        name_List_back[i].setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)


def SetCellWidget(widget, list_):
    for idx, i in enumerate(list_):
        widget.setCellWidget(idx, 1, i)


def SetReadOnly(widget_list):
    for i in widget_list:
        i.setReadOnly(True)

# def Table_widget_setItem(widget,name_list):
#     name_List_back = []
#     for i in name_list:
#         name = QTableWidgetItem(i)
#         name_List_back.append(name)
#     for i in range(len(name_List_back)):
#         name_List_back[i].setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)

def SaveDirPATH(widget,lineEdit):
    if lineEdit.text():
        fir_dir = lineEdit.text()
    else:
        fir_dir = ""

    path = str(QFileDialog.getExistingDirectory(widget,'%s-Open Directory' % __appname__,
                    fir_dir,QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks))
    path = path.replace('/','\\')
    fir_dir = fir_dir.replace('/','\\')
    if path == "" and fir_dir == "":
        lineEdit.setText(path)
        return
    if path == "" and fir_dir != "":
        return
    lineEdit.setText(path)

def SaveDirPATHs(widget,lineEdit):
    if lineEdit.text():
        fir_dir = lineEdit.text()
    else:
        fir_dir = ""

    path = str(QFileDialog.getExistingDirectory(widget,'%s-Open Directory' % __appname__,
                    fir_dir,QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks))
    path = path.replace('/','\\')
    fir_dir = fir_dir.replace('/','\\')
    if path == "":
        return
    elif path != "" and fir_dir == '':
        lineEdit.setText(path)
    elif path != "" and fir_dir != '':
        path = fir_dir+','+path
        lineEdit.setText(path)


def lineEdit_act_file(self,fileclass):  #打开文件
    act = QAction(self)
    act.setIcon(QIcon(':icons/1-1.ico'))
    EditPath_act = QLineEdit('')
    act.triggered.connect(lambda: SaveDirFile(self,EditPath_act,fileclass))
    EditPath_act.addAction(act, QLineEdit.TrailingPosition)
    return EditPath_act


def SaveDirFile(widget,lineEdit,fileclass):
    if lineEdit.text():
        fir_dir = lineEdit.text()
    else:
        fir_dir = ""

    path = QFileDialog.getOpenFileName(widget,'%s-Open Directory' % "__appname__",
                                        fir_dir,fileclass)
    path  = str(path[0])
    path = path.replace('/','\\')
    fir_dir = fir_dir.replace('/','\\')
    if path == "" :
        lineEdit.setText(fir_dir)
        return
    if path != "" :
        lineEdit.setText(path)
        return


def lineEdit_act_files(self,fileclass):  #打开文件
    act = QAction(self)
    act.setIcon(QIcon(':icons/1-1.ico'))
    EditPath_act = QLineEdit('')
    act.triggered.connect(lambda: SaveDirFiles(self,EditPath_act,fileclass))
    EditPath_act.addAction(act, QLineEdit.TrailingPosition)
    return EditPath_act


def SaveDirFiles(widget,lineEdit,fileclass):
    if lineEdit.text():
        fir_dir = lineEdit.text()
    else:
        fir_dir = ""

    path = QFileDialog.getOpenFileName(widget,'%s-Open Directory' % "__appname__",
                                        fir_dir,fileclass)
    path  = str(path[0])
    path = path.replace('/','\\')
    fir_dir = fir_dir.replace('/','\\')

    if path == "":
        return
    elif path != "" and fir_dir == '':
        lineEdit.setText(path)
    elif path != "" and fir_dir != '':
        path = path+','+fir_dir
        lineEdit.setText(path)


def createLabel(self,tittle,color,alignment):
    qlabel = QLabel(tittle)
    qlabel.setFont(QFont(FONTSTR,FONT_SIZE3))
    qlabel.setAutoFillBackground(True)
    qlabel.setContentsMargins(30,3,30,3)
    palette = QPalette()
    palette.setColor(QPalette.Window,color)
    qlabel.setPalette(palette)
    qlabel.setAlignment(alignment)
    return qlabel



def createFilePathWidget_NObutton(self,labelTitle):
    pathLabel = createLabel_NObutton(self,labelTitle, Qt.gray, Qt.AlignCenter)
    pathLabel.setFont(QFont("Arial", 11))
    pathLabel.setMinimumWidth(50)
    pathLabel.setContentsMargins(2, 2, 2, 2)
    LineEdit = QLineEdit()
    LineEdit.setFont(QFont("Arial", 11))
    pathLayout = QHBoxLayout()
    pathLayout.setSpacing(0)
    pathLayout.setContentsMargins(0,3,0,3)
    pathLayout.addWidget(pathLabel, 13, Qt.AlignLeft)
    pathLayout.addWidget(LineEdit, 90, Qt.AlignTop)
    pathContainer = QWidget()
    pathContainer.setLayout(pathLayout)
    return pathLabel,LineEdit,pathLayout,pathContainer

def createLabel_NObutton(self, tittle, color, alignment):
    qlabel = QLabel(tittle)
    qlabel.setFont(QFont("Arial", 11))
    qlabel.resize(140, 20)
    qlabel.setAutoFillBackground(True)
    qlabel.setContentsMargins(30, 3, 30, 3)
    palette = QPalette()
    palette.setColor(QPalette.Window, color)
    qlabel.setPalette(palette)
    qlabel.setAlignment(alignment)
    return qlabel

def Tasklistwidget():
    contentsWidget = QListWidget()
    contentsWidget.clear()
    contentsWidget.setViewMode(QListView.IconMode)
    contentsWidget.setMovement(QListView.Static)
    contentsWidget.setIconSize(QSize(196, 120))
    contentsWidget.setSpacing(20)  # 间距大小
    contentsWidget.setViewportMargins(45, 0, 10, 0)  # 间距大小
    return contentsWidget

def TextBrowserBulid():
    TextBrowser = QTextBrowser()   #消息框
    TextBrowser.document().setMaximumBlockCount(100)
    TextBrowser.setMaximumHeight(100)
    TextBrowser.setLineWrapMode(0)
    TextBrowser.moveCursor(TextBrowser.textCursor().StartOfLine)
    return TextBrowser

def Find_Device(widget,device_list):
    widget.AddDevice(device_list)
    if len(device_list) > 1:
        widget.select_all()